package org.vestbjergbyggecenter.Model;

import java.util.ArrayList;

public class ContractorRecords {
	public static ArrayList<Contractor> contractors;
	private static ContractorRecords instance=null;
	
	//Constructor
	public ContractorRecords()
	{
		contractors=new ArrayList<Contractor>();
	}
	
	//Method for the instance creation.
	public static ContractorRecords getInstance()
	{
		if(instance==null)
		{
			instance=new ContractorRecords();
		}
		return instance;
	}
	
	//Method to view the elements of the contractors Array List
	public void viewContractorList()
	{
		System.out.println("Contractor list:");
		for(Contractor contractor:contractors)
		{
			System.out.println(" ");
			System.out.println("Contractor ID: " + contractor.getContractorID() + " " + "Contractor name: " + contractor.getContractorName() + " " + "Contractor CVR: " + contractor.getContractorCVR() + "Contact person name: " + contractor.contactPerson.getName());
		}
	}

	/*
	 * Method checks if a contractor is in the contractors Array List and returns it.
	 * id based
	 */
	public static  Contractor getContractor(int wantedID)
	{
		Contractor wantedContractor=null;
		for(Contractor contractor: contractors)
		{
			if(contractor.getContractorID()==wantedID)
			{
				wantedContractor=contractor;
			}			
		}
		if(wantedContractor==null)
		{
			System.out.println("There is no contractor by this ID.");
		}
		return wantedContractor;
	}	
	
	/*
	 * Method to add a contractor to the contractors Array List.
	 * Contractors added from the system.
	 */
	public void addContractor(Contractor contractor)
	{
		contractors.add(contractor);
	}
	
	/*
	 * Method removes a contractor from the contractors Array List.
	 * index based
	 */
	public void removeContractorIndex(int index)
	{
		contractors.remove(index);
	}
	
	/*
	 * Method removes a contractor from the contractors Array List.
	 * id based
	 */
	public void removeContractorID(int wantedID)
	{
		for(Contractor contractor: contractors)
		{
			if(contractor.getContractorID()==wantedID)
			{
				contractors.remove(contractor);
			}
		}
	}
	
	/*
	 * Method updates contractor.
	 * Id based.
	 */
	public void updateContractor(int contractorID, long newCVR, String newContractorName, String newContactPersonName)
	{
		Contractor changeContractor=getContractor(contractorID);
		if(changeContractor!=null)
			{
				changeContractor.setContractorName(newContractorName);
				changeContractor.setContractorCVR(newCVR);
				changeContractor.contactPerson.setName(newContactPersonName);
			}
	}

}
